import bcryptjs from 'bcryptjs';
import { menuItem } from './myinfoType';
import { db } from '../../database/index';
import { Request, Response } from 'express';

export const myinfo = {
  // 修改密码接口
  changePassword: (req: Request, res: Response) => {
    const userinfo = req.body;
    console.log(req.body);
    userinfo.password = bcryptjs.hashSync(userinfo.password, 10);
    const sqlChangePwd = `update ev_user set password=? where username=?`;

    db.query(
      sqlChangePwd,
      [userinfo.password, userinfo.username],
      (err, result) => {
        if (err) return res.send(err);
        if (result.affectedRows !== 1) return res.send({
          status: 1,
          message: '修改密码失败，请重试！',
          data: null
        })

        res.send({
          status: 0,
          message: '修改密码成功！',
          data: null
        });
      });
  }
}